package com.woniuxy01;

import java.sql.*;
import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-08-08 17:25
 */
public class Test02 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入学号");
        int sid = sc.nextInt();
        System.out.println("输入姓名");
        String sname = sc.next();
        System.out.println("输入密码");
        String password = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
        Connection connection = JdbcUtil.getConnection();
        String sql = "insert into stu values(?,?,?,?)";
        Object[] objs={sid,sname,password,age};
        int count = JdbcUtil.executeUpdate(connection, sql, objs);
        System.out.println(count);
    }

    private static void test02() throws ClassNotFoundException, SQLException {
        Scanner sc = new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db2";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入用户名");
        String sname = sc.nextLine();
        System.out.println("输入密码");
        String pwd = sc.nextLine();
//        String sql="select * from stu where sname='"+sname+"' and password='"+pwd+"'";
//        Statement statement = connection.createStatement();
        String sql="select * from stu where sname=? and password=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, sname);
        statement.setString(2, pwd);
        ResultSet resultSet = statement.executeQuery();
        if(resultSet.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
    }

    private static void test01() throws ClassNotFoundException, SQLException {
        Scanner sc = new Scanner(System.in);
        String className = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String password = "123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("输入学号");
        int sid = sc.nextInt();
        System.out.println("输入姓名");
        String sname = sc.next();
        System.out.println("输入密码");
        String pwd = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
//        String sql = "insert into stu values(" + sid + " '" + sname + "'," + age + ")";
        //这个方法不需要sql参数
//        Statement statement = connection.createStatement();
        //执行才使用sql
//        int count = statement.executeUpdate(sql);
        //使用?作为值的占位符
        String sql="insert into stu values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //填充占位符对应的值
        preparedStatement.setInt(1, sid);
        preparedStatement.setString(2, sname);
        preparedStatement.setString(3, pwd);
        preparedStatement.setInt(4, age);
        int count = preparedStatement.executeUpdate();
        System.out.println("count=" + count);
        preparedStatement.close();
        connection.close();
    }
}
